package com.hx.mypetstore.ui.Login;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.hx.mypetstore.R;
import com.hx.mypetstore.constant.SPConstants;
import com.hx.mypetstore.network.HttpURL;
import com.hx.mypetstore.util.SPUtil;
import com.hx.mypetstore.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class VerifyActivity extends AppCompatActivity {

    // 手机号
    private AppCompatEditText mEtPhone;
    // 密码
    private AppCompatEditText mEtPassword;
    private String TAG = "绑定页面";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify);

        //初始化
        init();
    }

    private void init() {
        // 顶部返回
        ImageView mIvBack = findViewById(R.id.title_back);

        mEtPhone = findViewById(R.id.verify_phone);
        mEtPassword = findViewById(R.id.verify_password);

        mIvBack.setOnClickListener(v -> {
            // 返回
            finish();
        });

        findViewById(R.id.verify_confirm).setOnClickListener(v -> {
            // 确定设置
            onConfirmSet();
        });
    }

    private void onConfirmSet() {
        String url = HttpURL.VERIFY;
        //发送请求
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("phone", mEtPhone.getText().toString())
                .add("password",mEtPassword.getText().toString())
                .add("openID", SPUtil.get(this, SPConstants.KEY_HW_OEPNID,"openID").toString())
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().string());
                    Log.e(TAG, "绑定结果"+jsonObject.toString() );
                    if(jsonObject.getString("statusCode").equals("200")) {
                        SPUtil.put(getApplication(),SPConstants.KEY_LOCAL_USER,mEtPhone.getText().toString());
                        finish();
                    }
                    else{
                        Looper.prepare();
                        ToastUtil.getInstance().showShort(getApplication(),jsonObject.getString("message"));
                        Looper.loop();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}